import {createRouter, createWebHashHistory} from "vue-router";
import Home from "../views/Home.vue";

const routes = [
    {
        path: '/',
        redirect: '/login'
    }, {
        path: "/",
        name: "Home",
        component: Home,
        children: [
            {
                path: "/dashboard",
                name: "dashboard",
                meta: {
                    title: '系统首页'
                },
                component: () => import ( /* webpackChunkName: "dashboard" */ "../views/Dashboard.vue")
            },{
                path: "/menu",
                name: "menu",
                meta: {
                    title: '菜单管理'
                },
                component: () => import ( /* webpackChunkName: "dashboard" */ "../views/Menu.vue")
            },
            {
                path: "/role",
                name: "role",
                meta: {
                    title: '角色管理'
                },
                component: () => import ( /* webpackChunkName: "dashboard" */ "../views/Role.vue")
            },
            {
                path: "/admin",
                name: "admin",
                meta: {
                    title: '管理员管理'
                },
                component: () => import ( /* webpackChunkName: "dashboard" */ "../views/Admin.vue")
            },
            {
                path: "/user",
                name: "user",
                meta: {
                    title: '普通用户管理'
                },
                component: () => import ( /* webpackChunkName: "dashboard" */ "../views/User.vue")
            },
            {
                path: "/dict",
                name: "addrdict",
                meta: {
                    title: '数据字典'
                },
                component: () => import("../views/Addrdict.vue")
            }, {
                path: "/hosp",
                name: "detection",
                meta: {
                    title: '检测点信息'
                },
                component: () => import("../views/Detection.vue")
            },
            {
                path: "/order",
                name: "order",
                meta: {
                    title: '预约信息管理'
                },
                component: () => import("../views/Order.vue")
            },
            {
                path: "/result",
                name: "result",
                meta: {
                    title: '检测结果管理'
                },
                component: () => import("../views/Result.vue")
            },
            {
                path: '/404',
                name: '404',
                meta: {
                    title: '找不到页面'
                },
                component: () => import (/* webpackChunkName: "404" */ '../views/404.vue')
            }, {
                path: '/403',
                name: '403',
                meta: {
                    title: '没有权限'
                },
                component: () => import (/* webpackChunkName: "403" */ '../views/403.vue')
            }, {
                path: '/my',
                name: 'my',
                meta: {
                    title: '个人中心'
                },
                component: () => import (/* webpackChunkName: "user" */ '../views/My.vue')
            },
        ]
    }, {
        path: "/login",
        name: "login",
        meta: {
            title: '登录'
        },
        component: () => import ( /* webpackChunkName: "login" */ "../views/Login.vue")
    }
];

const router = createRouter({
    history: createWebHashHistory(),
    routes
});

router.beforeEach((to, from, next) => {
    document.title = `${to.meta.title} | vue-manage-system`;
    const role = sessionStorage.getItem('username');
    if (!role && to.path !== '/login') {
        next('/login');
    } else if (to.meta.permission) {
        // 如果是管理员权限则可进入，这里只是简单的模拟管理员权限而已
        role === 'admin'
            ? next()
            : next('/403');
    } else {
        next();
    }
});

export default router;